package com.ktg.web.mainData;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktg.ktgcommon.domain.mainData.MdClient;
import com.ktg.ktgcommon.domain.mainData.MdItem;
import com.ktg.ktgcommon.query.QueryClient;
import com.ktg.ktgcommon.result.R;
import com.ktg.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class ClientManagerController {

    @Autowired
    private ClientService clientService;

    //条件查询
    @PostMapping("/client/checkListClient")
    public R checkListClient(String clientCode, String clientName,
                             String clientNick, String clientEn,
                             String enableFlag,
                             @RequestParam(defaultValue = "1") Integer pageNo,
                             @RequestParam(defaultValue = "5") Integer pageSize) {
        QueryClient queryClient = new QueryClient();
        //设置参数
        queryClient.setClientCode(clientCode);
        queryClient.setClientNick(clientNick);
        queryClient.setClientName(clientName);
        queryClient.setEnableFlag(enableFlag);
        queryClient.setClientEn(clientEn);

        System.out.println(queryClient);

        Page<MdClient> page = new Page<>(pageNo, pageSize);

        Page<MdClient> selectPage = clientService.selectPage(page, queryClient);
        return R.OK(selectPage);
    }

    //根据id查询单条数据
    @PostMapping("/client/checkClientById/{clientId}")
    public R checkClientById(@PathVariable Integer clientId) {
        MdClient mdClient = clientService.checkClientById(clientId);
        return R.OK(mdClient);
    }

    //根据是否传递进来id判断是修改还是新增
    @PostMapping("/client/AddOrChClient")
    public R AddOrChClient(MdClient mdClient) {
        Integer integer = clientService.AddOrChClient(mdClient);
        return R.OK(integer);
    }

    //根据传递进来的id进行删除
    @DeleteMapping("/client/delClient/{clientId}")
    public R delClient(@PathVariable("clientId") Integer clientId) {
        Integer integer = clientService.delClient(clientId);
        return R.OK(integer);
    }
    //根据传递进来的集合进行批量删除
    @DeleteMapping("/client/delClientIdArray")
    public R delClientIdArray(@RequestBody List<Integer> arrayClientId){
        Integer integer = clientService.delClientIdArray(arrayClientId);
        return R.OK(integer);
    }
}
